package com.cmwa.solution.business.archiveManagement.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.archiveManagement.service.ArchiveManagementService;
import com.cmwa.solution.exception.ValidateFailedException;
import com.cmwa.solution.sys.attach.dao.AttachDao;
import com.cmwa.solution.sys.attach.dao.AttachRelDao;
import com.cmwa.solution.sys.attach.dao.AttachRelExtDao;
import com.cmwa.solution.sys.attach.entity.AttachRelExtVo;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.utils.SessionUtils;

/**
 * 档案管理业务实现层
 * 
 * @author ex-chenxh
 * @date 2019-01-22 10:30:12
 */
@Service
public class ArchiveManagementServiceImpl implements ArchiveManagementService {
	private static final Log log = LogFactory.getLog(ArchiveManagementServiceImpl.class);
	@Resource
	private AttachDao attachDao;
	@Resource
	private AttachRelDao attachRelDao;
	@Resource
	private AttachRelExtDao attachRelExtDao;
	
	@Override
	public void insertArchiveManagement(List<Map<String,String>> archiveList) {
		//获取当前登录员工
		String createId = SessionUtils.getEmployee().getID();
		//组装批量入库信息
		List<AttachRelExtVo> list = new ArrayList<AttachRelExtVo>();
		AttachRelExtVo attachRelExtVo = null;
		String attId = null;
		String relId = null;
		String relType = null;
		String attrName = null;
		String attrValue = null;
		for (Map<String,String> map : archiveList) {
			attId = map.get("attId");
			relId = map.get("relId");
			relType = map.get("relType");
			attrName = map.get("attrName");
			attrValue = map.get("attrValue");
			attachRelExtVo = new AttachRelExtVo(null, null, attrName, attrValue, null, createId, null, null, null, attId, relId, relType);
			list.add(attachRelExtVo);
		}
		//批量入库
		attachRelExtDao.batchInsertAttachRelAndExt(list);
	}

	@Override
	public int  deleteArchiveManagement(String ids) {
		return attachRelDao.batchDeleteAttachRelByAttId(ids);
	}
	
	@Override
	public void updateArchiveManagement(List<Map<String,String>> archiveList) {
		//获取当前登录用户主键
		String modifyId = SessionUtils.getEmployee().getID();
		String oldPrjCode = null;
		String relId = null;
		String attId = null;
		String relType = null;
		String extId = null;
		String attrName = null;
		String attrValue = null;
		//获取需要删除附件的主键
		String attDelId = null;
		
		AttachVo attachVo = null;
		for (Map<String, String> map : archiveList) {
			//获取前端传递过来的参数
			oldPrjCode = map.get("oldPrjCode");
			relId = map.get("relId");
			attId =  map.get("attId");
			relType =  map.get("relType");
			extId =  map.get("extId");
			attrName =  map.get("attrName");
			attrValue =  map.get("attrValue");
			attDelId = map.get("attDelId");
			//获取当前数据是否存在数据库中
			attachVo = attachDao.getAttAndRelAndExtById(attId);
			//如果当前附件关联关系不存在数据库中，则新增
			if(null == attachVo) {
				//添加新数据的关联关系
				List<AttachRelExtVo> newAttRelExt = new ArrayList<AttachRelExtVo>();
				AttachRelExtVo attachRelExtVo = new AttachRelExtVo(null, null, attrName, attrValue, null, modifyId, null, modifyId, null, attId, relId, relType);
				
				newAttRelExt.add(attachRelExtVo);
				attachRelExtDao.batchInsertAttachRelAndExt(newAttRelExt);
			}else {
				//修改原有的关联关系
				String tempPrjCode = attachVo.getPrjCode();
				if(StringUtils.isNotBlank(relId) && StringUtils.isNotBlank(oldPrjCode) ) {
					if(oldPrjCode.equals(tempPrjCode)){
						//传递进来的产品代码和原来的产品代码不一致，则做修改
						if(!relId.equals(tempPrjCode)){
							attachDao.updateRelIdByAttRelId(relId,modifyId,attachVo.getAttRelId());
						}
					}else {
						log.error("前台传递过来的原产品代码与数据库中保存的产品代码不一致！");
						throw new ValidateFailedException("前台传递过来的原产品代码与数据库中保存的产品代码不一致！");
					}
				}else {
					log.error("需要修改的产品代码不能为空：prjCode："+relId+"；oldPrjCode："+oldPrjCode);
					throw new ValidateFailedException("需要修改的产品代码不能为空：prjCode："+relId+"；oldPrjCode："+oldPrjCode);
				}
				//根据拓展字段ID获取拓展信息
				AttachRelExtVo attachInfoExtVo = attachRelExtDao.getAttachRelExtById(extId);
				if(null != attachInfoExtVo) {
					//修改原有拓展字段关联关系
					attachInfoExtVo.setAttrName(attrName);
					attachInfoExtVo.setAttrValue(attrValue);
					attachInfoExtVo.setModifyId(modifyId);
					attachRelExtDao.updateAttachRelExtById(attachInfoExtVo);
				}else {
					//添加新的拓展字段关联关系
					attachInfoExtVo = new AttachRelExtVo();
					attachInfoExtVo.setAttRelId(attachVo.getAttRelId());
					attachInfoExtVo.setAttrName(attrName);
					attachInfoExtVo.setAttrValue(attrValue);
					attachInfoExtVo.setCreateId(modifyId);
					attachRelExtDao.insertAttachRelExt(attachInfoExtVo);
				}
			}
		}
		//删除需要删除的附件
		if(StringUtils.isNotBlank(attDelId)) {
			attachRelDao.batchDeleteAttachRelByAttId(attDelId);
		}
	}

	@Override
	public AttachVo getArchiveManagementById(String attId) {
		return attachDao.getAttAndRelAndExtById(attId);
	}

	@Override
	public Page<AttachVo> archiveManagementListPage(SearchParam sp) {
		String empId = SessionUtils.getEmployee().getID();
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			sp.getSp().put("empId", empId);
		}
		List<AttachVo> items = attachDao.attAndRelAndExtListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}
}
